//
//  ChoiceVoiceViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 17/1/14.
//  Copyright © 2017年 com.xtion.crm. All rights reserved.
//

#import "ChoiceVoiceViewController.h"
#import "XHSoundRecorder.h"

NSString *const ChoiceVoiceViewControllerChoiceVoiceParamKey = @"ChoiceVoiceViewControllerChoiceVoiceParamKey";

@interface ChoiceVoiceViewController ()<XUIButtonDelegate>
{
    UIView *_voicePanel;
    UIView *_btnPanel;
    XUIButton *_leftBtn;
    XUIButton *_rightBtn;
    UIView *_circleView;
    UILabel *_timeLabel;
    
    XMSTimerBlock *_timeBlock;
    NSInteger _timeCount;
}
@end

@implementation ChoiceVoiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    [self setBackNavigateItemTitle:@"录音" rightItemText:@"确定"];
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    
    _voicePath = @"";
    
    [self initUI];
}

- (void)initUI
{
    _voicePanel = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height - 64 - 60)];
    [self.view addSubview:_voicePanel];
    
    _circleView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 180, 180)];
    _circleView.layer.cornerRadius = 90;
    _circleView.layer.masksToBounds = YES;
    _circleView.layer.borderWidth = 2.0;
    _circleView.layer.borderColor = [[UIColor colorWithHex:mcrmSAASUIShinColorTextStyle2196F3Color] CGColor];
    _circleView.centerX = self.view.width/2;
    [_voicePanel addSubview:_circleView];
    
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
    _timeLabel.center = CGPointMake(90, 90);
    _timeLabel.font = [UIFont systemFontOfSize:18];
    _timeLabel.text = @"00:00";
    _timeLabel.textAlignment = NSTextAlignmentCenter;
    [_circleView addSubview:_timeLabel];
    
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _circleView.bottom + 15, 180, 50)];
    tipLabel.centerX = self.view.width/2;
    tipLabel.font = [UIFont systemFontOfSize:15];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.text = @"单击开始录音";
    [_voicePanel addSubview:tipLabel];
    
    _btnPanel = [[UIView alloc] initWithFrame:CGRectMake(0, _voicePanel.bottom, self.view.width, 60)];
    _btnPanel.backgroundColor = [UIColor colorWithHex:mcrmSAASUIShinColorTextStyle2196F3Color];
    [self.view addSubview:_btnPanel];
    
    _leftBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeLeftHeadImage delegate:self];
    _leftBtn.size = CGSizeMake(100, 50);
    [_leftBtn SetFont:[UIFont systemFontOfSize:13]];
    _leftBtn.delegate = self;
    _leftBtn.centerX = self.view.width/4;
    _leftBtn.centerY = _btnPanel.height/2;
    [_leftBtn SetTitle:@"开始录音"];
    [_leftBtn SetTitleColor:[UIColor whiteColor]];
    [_btnPanel addSubview:_leftBtn];
    
    _rightBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeLeftHeadImage delegate:self];
    _rightBtn.size = CGSizeMake(100, 50);
    [_rightBtn SetFont:[UIFont systemFontOfSize:13]];
    _rightBtn.delegate = self;
    _rightBtn.centerX = self.view.width*3/4;
    _rightBtn.centerY = _btnPanel.height/2;
    [_rightBtn SetTitle:@"完成录音"];
    [_rightBtn SetTitleColor:[UIColor whiteColor]];
    [_btnPanel addSubview:_rightBtn];
}

-(void)viewControllerWillMoveDealloc
{
    [super viewControllerWillMoveDealloc];
    
    [_timeBlock invalidate];
    _timeBlock = nil;
    
    [XUINavigationController enableSwipeToLeftViewController:YES];
}

-(void)clickedBackBarButton:(id)sender
{
    [_timeBlock invalidate];
    _timeBlock = nil;
    
    [XUINavigationController pop];
}

-(void)clickedUpInsideOfButton:(XUIButton *)btn
{
    [UIViewController closeKeyBoard];
    
    NSString *title = btn.title;
    if(btn == _leftBtn)
    {
        if([title isEqualToString:@"开始录音"])
        {
            [_leftBtn SetTitle:@"录音中..."];
            [_rightBtn SetTitle:@"暂停录音"];
            
            _timeCount = 0;
            if(_timeBlock == nil)
            {
                _timeBlock = [[XMSTimerBlock alloc]initScheduledTimerWithTimeInterval:1 repeats:YES block:^(XMSTimerBlock *timer)
                              {
                                  _timeCount++;
                                  _timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld",_timeCount/60,_timeCount%60];
                              }];
                [self recorder];
            }
        }
        else if([title isEqualToString:@"继续录音"])
        {
            [_leftBtn SetTitle:@"录音中..."];
            [_rightBtn SetTitle:@"暂停录音"];
            
            [_timeBlock setFireDate:[NSDate date]];
            [self recorder];
        }
    }
    else if(btn == _rightBtn)
    {
        if([title isEqualToString:@"暂停录音"])
        {
            [_leftBtn SetTitle:@"继续录音"];
            [_rightBtn SetTitle:@"完成录音"];
            
            [_timeBlock setFireDate:[NSDate distantFuture]];
            [self pauseRecorder];
        }
        else if([title isEqualToString:@"完成录音"])
        {
            if(_timeBlock != nil)
            {
                [_timeBlock invalidate];
                _timeBlock = nil;
                
                [self stopRecorder];
                
                [XUIAlertView alertViewWithTitle:@"提示" message:@"是否保存该录音文件" cancelButtonTitle:@"保存" otherButtonTitles:@[@"放弃"] clickBlock:^(NSInteger nClickindex)
                 {
                     if(nClickindex == 0)
                     {
                         [_leftBtn SetTitle:@"开始录音"];
                         [_rightBtn SetTitle:@"播放"];
                     }
                     else if(nClickindex == 1)
                     {
                         _voicePath = @"";
                         _timeLabel.text = @"00:00";
                         [_leftBtn SetTitle:@"开始录音"];
                         [_rightBtn SetTitle:@"完成录音"];
                     }
                 }];
            }
        }
        else if([title isEqualToString:@"播放"])
        {
            [[XHSoundRecorder sharedSoundRecorder] playsound:self.voicePath withFinishPlaying:nil];
        }
    }
}

- (void)recorder
{
    typeof(self) wSelf = self;
    
    [[XHSoundRecorder sharedSoundRecorder] startRecorder:^(NSString *filePath) {
        NSLog(@"录音文件路径:%@",filePath);
        NSLog(@"录音结束");
        
        wSelf.voicePath = filePath;
    }];
}

- (void)pauseRecorder
{
    [[XHSoundRecorder sharedSoundRecorder] pauseRecorder];
}

- (IBAction)stopRecorder
{
    [[XHSoundRecorder sharedSoundRecorder] stopRecorder];
}

-(void)clickedRightBarButton:(id)sender
{
    if([self.voicePath isEqualToString:@""])
    {
        [UIAlertView showInfoMsg:@"没有可以播放的语音"];
        return;
    }
    XUIViewControllerSwitchParam *p = [XUIViewControllerSwitchParam new];
    p.param[ChoiceVoiceViewControllerChoiceVoiceParamKey] = _voicePath;
    [XUINavigationController popWithSwitchParam:p];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
